package org.leumasjaffe.initiativeTracker.distributed.controller;

import static org.leumasjaffe.initiativeTracker.operator.InitiativeController.tracker;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.leumasjaffe.initiativeTracker.entity.CharacterList;
import org.leumasjaffe.initiativeTracker.operator.InitiativeController;

import edu.wpi.heineman.xml.Message;

@NonNullByDefault
public class UploadHandler {

	@SuppressWarnings("null")
	public Message process(Message request) {
		return process(request.id(), request.contents.getFirstChild().getAttributes().getNamedItem("game")
				.getNodeValue());
	}
	
	public Message process(String id, String game) {
		InitiativeController tracker = tracker(game);
		String xmlString = Message.responseHeader(id) +
				"<initiativeResponse game='" + game + "' turn='" + tracker.root.getTurnNum() +
				"' next='" + tracker.root.getSubTurn() + "'>";

		CharacterList list = tracker.list;
		for (int i = 0; i < list.size(); i++) {
			String characterXML = list.get(i).getCharacter().serialize();
			xmlString += characterXML;
		}

		xmlString += "</initiativeResponse></response>";

		Message send = new Message(xmlString);
		return send;
	}
	
}
